package com.ruoyi.personnel.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.personnel.domain.Transfer;
import com.ruoyi.personnel.service.IPersonnelService;
import com.ruoyi.personnel.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/transfer")
public class TransferController extends BaseController {

    @Autowired
    private TransferService transferService;

    @Autowired
    private IPersonnelService personnelService;

    /**
     * 根据人员id查询信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public AjaxResult getTransferByUId(@PathVariable Integer userId) {
        Transfer transfer = transferService.getTransferByUId(userId);
        if(transfer !=null)
        {
            return AjaxResult.success(transfer);
        }
        else
        {
            return AjaxResult.warn("还未认证，请认证！！");
        }
    }

    /**
     * 根据用户输入的身份证等信息，查询数据库是否存在，存在则插入接口表，否则不插入
     * @param transfer
     * @return
     */
    @PostMapping
    public AjaxResult insTransfer(@RequestBody Transfer transfer) {
        System.out.println(transfer);
        try{
            if (personnelService.selectPersonnelByIDcard(transfer.getUserIdCard())!=null && personnelService.selectPersonnelByIDcard(transfer.getUserIdCard()).getpName().equals(transfer.getUserName())) {
                return toAjax(transferService.insertTransfer(transfer));
            }
            return AjaxResult.warn("身份证不存在或已认证，请确认信息后重新输入!!");
        }catch (DuplicateKeyException e)
        {
            return AjaxResult.warn("身份证不存在或已认证，请确认信息后重新输入!!");
        }
    }



}
